<script setup lang="ts">
import {ref} from 'vue';
import {useRouter} from "vue-router";

const searchText = ref('')
const router = useRouter()
const show = ref(true);
// 已选标签的数组
const activeIds = ref([]);
// 左侧选中项的索引
const activeIndex = ref(0);
const originTagList = [
  {
    text: '性别',
    children: [
      {text: '男', id: '男'},
      {text: '女', id: '女'},
    ],
  },
  {
    text: '年级',
    children: [
      {text: '大一', id: '大一'},
      {text: '大二', id: '大二'},
      {text: '大三', id: '大三'},
      {text: '大四', id: '大四'},
      {text: '研一', id: '研一'},
      {text: '研二', id: '研二'},
      {text: '研三', id: '研三'},
    ],
  }
]
const tagList = ref(originTagList);
const onSearch = () => {
  tagList.value = originTagList.map(parentTag => {
    const tempChildren = [...parentTag.children];
    const tempParentTag = {...parentTag};
    tempParentTag.children = tempChildren.filter(item => item.text.includes(searchText.value))
    return tempParentTag
  })
}
const onCancel = () => {
  searchText.value = ''
  tagList.value = originTagList.value
}
const removeTag = (tag) => {
  activeIds.value = activeIds.value.filter(item => {
    return item !== tag
  })
};
const doSearchResult = () => {
  router.push({
    path: '/user/list',
    query: {
      tags: activeIds.value
    }
  })
}
</script>

<template>
  <form action="/">
    <van-search
        v-model="searchText"
        show-action
        placeholder="请输入用户标签"
        @search="onSearch"
        @cancel="onCancel"
    />
  </form>
  <van-divider content-position="center">已选标签</van-divider>
  <van-tag mark size="large" v-if="activeIds.length === 0 ">暂无已选标签</van-tag>
  <van-row gutter="20" style="padding: 0 20px">
    <van-col v-for="tag in activeIds" style="padding-bottom: 10px">
      <van-tag style="margin-bottom: 3px" :show="show" closeable plain round size="large" type="primary"
               @close="removeTag(tag)">
        {{ tag }}
      </van-tag>
    </van-col>
  </van-row>
  <van-divider content-position="center">选择标签</van-divider>
  <van-tree-select
      v-model:active-id="activeIds"
      v-model:main-active-index="activeIndex"
      :items="tagList"
  />
  <div style="padding: 16px">
    <van-button block type="primary" @click="doSearchResult">搜索</van-button>
  </div>

</template>

<style scoped lang="scss">

.van-row {
  :deep(.van-col) {
    padding-left: 0 !important;
    padding-right: 15px !important;
  }
}
</style>